<template>
  <el-button v-if="exportConfig.show" icon="el-icon-download" type="primary" @click="handleExport">导出</el-button>
</template>

<script>

export default {
  props: {
    exportConfig: {
      type: Object,
      default() {
        return {}
      }
    },
    form: {
      type: Object,
      default() {
        return {}
      }
    }
  },

  methods: {
    async handleExport() {
      try {
        const { api, beforeExport } = this.exportConfig
        if (!api) throw new Error('请配置导出功能对应的api接口')
        this.$confirm('是否确认要导出数据表？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        })
          .then(async() => {
            const query = typeof beforeExport === 'function' ? beforeExport(this.form) : this.form
            await api(query)
          })
          .catch((error) => {
            if (error !== 'cancel') {
              console.error(error)
            }
          })
      } catch (error) {
        console.error('【导出功能异常】', error)
      }
    }
  }

}
</script>
